import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 路由配置
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
    meta: { title: '首页', requiresAuth: true }
  },
  {
    path: '/splash',
    name: 'Splash',
    component: () => import('@/views/Splash.vue'),
    meta: { title: '欢迎', requiresAuth: false }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { title: '注册', requiresAuth: false }
  },
  {
    path: '/navigation',
    name: 'Navigation',
    component: () => import('@/views/Navigation.vue'),
    meta: { title: '场地导航', requiresAuth: true }
  },
  {
    path: '/performances',
    name: 'PerformanceList',
    component: () => import('@/views/PerformanceList.vue'),
    meta: { title: '演出列表', requiresAuth: true }
  },
  {
    path: '/performances/:id',
    name: 'PerformanceDetail',
    component: () => import('@/views/PerformanceDetail.vue'),
    meta: { title: '演出详情', requiresAuth: true }
  },
  {
    path: '/artists',
    name: 'ArtistList',
    component: () => import('@/views/ArtistList.vue'),
    meta: { title: '艺人列表', requiresAuth: true }
  },
  {
    path: '/artists/:id',
    name: 'ArtistDetail',
    component: () => import('@/views/ArtistDetail.vue'),
    meta: { title: '艺人详情', requiresAuth: true }
  },
  {
    path: '/interaction',
    name: 'FanInteraction',
    component: () => import('@/views/FanInteraction.vue'),
    meta: { title: '粉丝互动', requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/Profile.vue'),
    meta: { title: '个人中心', requiresAuth: true }
  },
  {
    path: '/notifications',
    name: 'Notifications',
    component: () => import('@/views/Notifications.vue'),
    meta: { title: '消息通知', requiresAuth: true }
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/Search.vue'),
    meta: { title: '搜索', requiresAuth: true }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: { title: '页面不存在' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  NProgress.start()
  
  const userStore = useUserStore()
  
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 音乐节助手` : '音乐节助手'
  
  // 检查是否需要登录
  if (to.meta.requiresAuth !== false) {
    // 检查是否有token
    if (!userStore.token) {
      // 如果未登录，跳转到登录页
      next('/login')
      return
    }
    
    // 如果有token但没有用户信息，尝试获取用户信息
    if (!userStore.user) {
      try {
        await userStore.fetchUserInfo()
      } catch (error) {
        // 获取用户信息失败，清除token并跳转到登录页
        userStore.token = ''
        userStore.user = null
        next('/login')
        return
      }
    }
  }
  
  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router 